﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using CourseDesign_02;

namespace FundamentalsOfCompiling
{
    class MidiatorInput : Midiator
    {
        MainWindow window;
        int tabs = 0;
        Lists lists = new Lists();

        public MidiatorInput(MainWindow main)
        {
            window = main;
            tabs = 0;
        }

        void Midiator.Write(TokenType token)
        {
            switch (token.Lex)
            {
                case LexType.PROGRAM:
                case LexType.PROCEDURE:
                case LexType.TYPE:
                case LexType.VAR:
                case LexType.IF:
                case LexType.THEN:
                case LexType.ELSE:
                case LexType.FI:
                case LexType.WHILE:
                case LexType.DO:
                case LexType.ENDWH:
                case LexType.BEGIN:
                case LexType.END:
                case LexType.READ:
                case LexType.WRITE:
                case LexType.ARRAY:
                case LexType.OF:
                case LexType.RECORD:
                case LexType.RETURN:
                case LexType.INTEGER:
                case LexType.CHAR1:
                    Write(window, token.Sem, Colors.Red);
                    break;
                case LexType.ASSIGN:
                case LexType.LT:
                case LexType.EQ:
                case LexType.PLUS:
                case LexType.MINUS:
                case LexType.TIMES:
                case LexType.OVER:
                case LexType.LPAREN:
                case LexType.RPAREN:
                case LexType.LMIDPAREN:
                case LexType.RMIDPAREN:
                    Write(window, token.Sem, Colors.Green);
                    break;

                case LexType.SEMI:// window.WriteLine(";"); break;
                case LexType.COLON:// window.WriteLine(":"); break;
                case LexType.COMMA:// window.WriteLine(","); break;
                case LexType.DOT:// window.WriteLine("."); break;
                case LexType.UNDERANGE:// window.WriteLine(".."); break;
                case LexType.ENDFILE1:// window.WriteLine("EOF"); break;
                case LexType.INTC:// window.WriteLine("INTC, val= " + token.Sem); break;
                case LexType.CHARC:// window.WriteLine("CHARC, letter= " + token.Sem); break;
                case LexType.ID: //window.WriteLine("ID, name= " + token.Sem); break;
                case LexType.ERROR1://window.WriteLine("ERROR: " + token.Sem); break;
                    Write(window, token.Sem, Colors.Black);
                    break;
                case LexType.CRT://window.WriteLine("记录的是换行符号"); break;
                    Write(window, "\n", Colors.Black);
                    break;
                default://window.WriteLine("Unknown token:" + token.Lex); break;
                    Write(window, token.Sem, Colors.Black);
                    break;
            }
        }

         void Write(MainWindow window, string str, Color color)
        {
            if (lists.leftTab.Contains(str) && (tabs > 0)) tabs--;
            if (lists.rightTab.Contains(str)) tabs++;
            window.rtbInput.AppendText(" " + str);
            if (str == "\n")
            {
                for (int i = 0; i < tabs; i++) window.rtbInput.AppendText("\t");
            }


            TextPointer tpEnd = window.rtbInput.Document.ContentEnd;
            TextPointer tpStart = tpEnd.GetNextInsertionPosition(LogicalDirection.Backward);
            for (int i = 0; i < str.Length; i++) tpStart = tpStart.GetNextInsertionPosition(LogicalDirection.Backward);
            TextRange tr = new TextRange(tpStart, tpEnd);
            tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
            bool right = lists.rightTab.Contains(str);
        }

        
         void Midiator.WriteLine(string str)
        {
            window.WriteLine(str);
        }

         void Midiator.Write(string str)
        {
            window.Write(str);
        }
    }
}
